CMAKE_MINIMUM_REQUIRED(VERSION 2.8.10)

PROJECT(zpsoe CXX)

OPTION(BUILD_TESTING "构建测试用例。" ON)

SET(ZPSOE_SHAREDLIB ON CACHE BOOL "如果值为 ON，zpsoe 将构建为共享库, 反之为静态库。")

IF(MSVC)
    # CMAKE_INSTALL_PREFIX default to c:/Program Files on Windows.
    # Use default CMAKE_INSTALL_PREFIX
ELSE()
    # CMAKE_INSTALL_PREFIX default to /usr/local on UNIX.
    SET(CMAKE_INSTALL_PREFIX /usr/local/zpsoe)
    SET(CMAKE_INSTALL_RPATH /usr/local/zpsoe/bin)
ENDIF()

INCLUDE(cmake/CompilerChecks.cmake)
INCLUDE(cmake/ConfigureChecks.cmake)
INCLUDE(cmake/CompilerFlags.cmake)

# zpsoeD.dll zpsoeD.so
SET(CMAKE_DEBUG_POSTFIX "D")

SET(OUTPUT_DIR ${CMAKE_BINARY_DIR}/bin)

# 由于单元测试需要，所有库和执行文件全部生成到目录 bin
IF(MSVC)
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR})
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR})
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL ${OUTPUT_DIR})
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO ${OUTPUT_DIR})

    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR})
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR})
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL ${OUTPUT_DIR})
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${OUTPUT_DIR})

    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR})
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR})
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${OUTPUT_DIR})
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${OUTPUT_DIR})
ELSE()
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIR})
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR})
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
ENDIF()

INCLUDE_DIRECTORIES("include")

# 所有使用 zpsoe 静态库的项目都必须定义 ZPSOE_STATICLIB 宏
IF(NOT ZPSOE_SHAREDLIB)
    ADD_DEFINITIONS("-DZPSOE_STATICLIB")
ENDIF()

# zpsoe 库规则 {
SET(ZPSOE_LIB_POSTFIX "")

IF(MSVC AND NOT ZPSOE_SHAREDLIB)
  # 增加静态编译库后缀：S
  SET(ZPSOE_LIB_POSTFIX ${ZPSOE_LIB_POSTFIX}S)
ENDIF() 

SET(ZPSOE_LIB_NAME zpsoe${ZPSOE_LIB_POSTFIX})

ADD_SUBDIRECTORY(src)
# }

# 单元测试规则 {
IF(BUILD_TESTING)
    # find gtest
    FIND_PACKAGE(GTest REQUIRED)
    INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})

    # 解决 GTest 编译报错：“std::tuple”: 模板 参数太多
    IF(MSVC)
    ADD_DEFINITIONS("-D_VARIADIC_MAX=10")
    ENDIF()

    ENABLE_TESTING()
    ADD_SUBDIRECTORY(test)
ENDIF()
# }